<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=$_G[charset]" />
<script type="text/javascript" src="{URLROOT}/data/cachefiles/config.js?{$_G[cfg][js_cache_flag]}"></script>
<script type="text/javascript" src="{URLROOT}/static/javascript/jquery.js"></script>
<script type="text/javascript" src="{URLROOT}/static/javascript/common.js?{$_G[modoer][build]}"></script>
<link rel="stylesheet" type="text/css" href="{URLROOT}/{$_G[tplurl]}css_common.css?{$_G[modoer][build]}" />
</head>
<body>
<script>
    $(document).ready(function() {

        if(WebSocket) {
            var socket = new WebSocket('ws://120.55.184.218:9555');
            socket.onerror = function(event) {
                console.log('websocket连接失败...启动AJAX轮询...');
                ajax_poll();
            }
            socket.onopen = function(event) { 
                console.log('websocket连接成功...');
                socket.send('LQL:{print G('session')->get_keyid()}'); 
                socket.onmessage = function(event) { 
                    console.log(event.data);
                    if(event.data == 'SCAN_OK:{print G('session')->get_keyid()}') {
                        poll_check();
                    }
                }; 
                socket.onclose = function(event) { 
                    console.log('与websocket服务器端端断开...启动AJAX轮询...');
                    ajax_poll();
                }; 
                //socket.close() 
            };
        } else {
            //浏览器不支持websocket
            console.log('与websocket服务器端端断开...启动AJAX轮询...');
            ajax_poll();
        }

        qrcode_show();
    });

    //ajax自动轮询
    function ajax_poll() {
        var ajax_poll_handle = null;
        ajax_poll_handle = setInterval(function() {
            poll_check(ajax_poll_handle);
        }, 3000);
    }

    //监测是否扫描成功
    function poll_check(Intervalhandle) {
        var url = Url('weixin/login_qrcode/op/poll/r/'+getRandom())
        $.get(url.url(), function(data) {
            console.debug(data);
            data = parse_json(data);
            if(data && data.code == '200') {
                if(data.scan) {
                    if(Intervalhandle) window.clearInterval(Intervalhandle);
                    console.log(data.bind?'账号存在/已绑定':'无账号/未绑定');
                    if(data.bind) {
                        if(data.login) {
                            var member_url = "{url member/index}";
                            window.parent.location.href = member_url.url();
                        } else {
                            alert('登录失败！请稍后尝试。');
                        }
                    } else {
                        console.log(url);
                        login_select();
                    }
                    return;
                }
                return;
            } else {
                if(Intervalhandle) window.clearInterval(Intervalhandle);
                alert('服务器通信失败，请刷新页面重试。');
            }
        });
    }

    function login_select() {
        console.log('login_select');
        var btnLogin = $('<a href="{url weixin/login_qrcode/op/auto_reg}" target="_top" class="abtn1 scan-select-btn">直接登录</a>');
        var btnBind = $('<a href="{url weixin/login_qrcode/op/bind}" target="_top" class="abtn2 scan-select-btn">绑定账号</a>');

        $('#qrcode_foo').empty().append("<p class=\"scan-select-p\">扫描成功！您是否在要绑定网站已存在账号？点击“绑定账号”进行绑定，选择“直接登录”，直接用微信账号登录网站。</p>")
        .append(btnLogin).append(btnBind);
    }

    //显示一张二维码
    function qrcode_show() {
        $.get(Url('weixin/login_qrcode/op/refresh/r/'+getRandom()), function(data) {
            var json = parse_json(data);
            if(json.code == 200) {
                $('#qrcode_foo >a').remove();
                $('#qrcode_img').attr('src', json.qrcode_url);
                //qrcode_timelimit();
                qrcode_timelife(); //有效期倒计时
            } else {
                alert('无法获取二维码，请使用其他方式登录。');
            }
        });
    }

    //二维码图片有效期定时
    function qrcode_timelife() {
        var time = 0;
        var max_time = {print ($ticket_time-10)};
        var img_timelife_handle = setInterval(function(){
            if(!$('#qrcode_img')[0]) {
                clearInterval(img_timelife_handle);
                return;
            }
            time++;
            if(time > max_time) {
                clearInterval(img_timelife_handle);
                qrcode_timelimit();
            }
        },1000);
    }

    function qrcode_timelimit() {
        var a = $('<a href="javascript:">二维码已过期，请点击刷新。</a>').css({
            position: 'absolute',
            top: 0, left:0, 
            width:'300px', height:'100%',
            background:'#eee',
            display:'block',
            fontSize:'15px',paddingTop:'100px',textAlign:'center',fontWeight:'bold',
            filter:'Alpha(opacity=90)', opacity: '0.9'
        }).click(function(event) {
            qrcode_show();
        });
        $('#qrcode_foo').append(a);
    }

</script>
<div id="qrcode_foo" style="position: relative;font-weight: ">
    <img src="$qrcode_url" id="qrcode_img" >    
</div>
<style>
.scan-select-p {
    margin: 20px 10px 80px;
    font-size: 14px;
    color:#888;
}
.scan-select-btn{
    display: block;
    margin: 10px;
    text-align: center;
    font-size: 16px;
    line-height:30px !important;
}
</style>
</body>
</html>
<!--{eval output();}-->